﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CongressVote.Models;

namespace CongressVote.Controllers
{ 
    public class _QuestionController : Controller
    {
        private finalEntities db = new finalEntities();

        //
        // GET: /_Question/

        public ViewResult Index()
        {
            var question = db.Question.Include(q => q.Users);
            return View(question.ToList());
        }

        //
        // GET: /_Question/Details/5

        public ViewResult Details(int id)
        {
            Question question = db.Question.Find(id);
            return View(question);
        }

        //
        // GET: /_Question/Create

        public ActionResult Create()
        {
            ViewBag.UserID = new SelectList(db.Users, "id", "name");
            return View();
        } 

        //
        // POST: /_Question/Create

        [HttpPost]
        public ActionResult Create(Question question)
        {
            if (ModelState.IsValid)
            {
                db.Question.Add(question);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.UserID = new SelectList(db.Users, "id", "name", question.UserID);
            return View(question);
        }
        
        //
        // GET: /_Question/Edit/5
 
        public ActionResult Edit(int id)
        {
            Question question = db.Question.Find(id);
            ViewBag.UserID = new SelectList(db.Users, "id", "name", question.UserID);
            return View(question);
        }

        //
        // POST: /_Question/Edit/5

        [HttpPost]
        public ActionResult Edit(Question question)
        {
            if (ModelState.IsValid)
            {
                db.Entry(question).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.UserID = new SelectList(db.Users, "id", "name", question.UserID);
            return View(question);
        }

        //
        // GET: /_Question/Delete/5
 
        public ActionResult Delete(int id)
        {
            Question question = db.Question.Find(id);
            return View(question);
        }

        //
        // POST: /_Question/Delete/5

        public ActionResult ViewQuestionsForSenator()
        {
            return View(db.Question.ToList());

        }
   
        [HttpPost]
        public ActionResult ViewQuestionsForSenator(string radioValue, int RQID)
        {
            if (ModelState.IsValid)
            {
                //Question_Vote myQuestion_vote = new Question_Vote();
                //myQuestion_vote.QID = RQID;
                //myQuestion_vote.AID = int.Parse(radioValue);

                //db.Entry(myQuestion_vote).State = EntityState.Added;
                Answer myAns = new Answer();
                myAns.Text = radioValue.ToString();
                Users myUser=new Users();
                int id;
                if (Session["UserID"] != null)
                {
                    id = (int)Session["UserID"];
                    myUser.id = id;
                    Question myquestion = db.Question.Find(RQID);
                    Question_Vote question_vote = new Question_Vote { Question = myquestion, Users = myUser, Answer = myAns, User_ID = id, QID = RQID, AID = int.Parse(radioValue) };

                    db.Question_Vote.Add(question_vote);
                    db.SaveChanges();
                    Dispose();
                }
                else
                    return RedirectToAction("Edit");
            }
            return RedirectToAction("Index");

        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Question question = db.Question.Find(id);
            db.Question.Remove(question);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}